SpringBoot整合redis,监控过期key,并对过期key进行处理 您所在的位置:网站首页 redis key过期时间为0 SpringBoot整合redis,监控过期key,并对过期key进行处理

SpringBoot整合redis,监控过期key,并对过期key进行处理

2024-06-30 16:16| 来源: 网络整理| 查看: 265

全网最简单的SpringBoo整合redis,监控过期key 1.首先在 pom.xml 文件中引入redis的相关依赖2.然后在 yml 文件中进行配置3.使用redis少不了配置类,下面的配置类可以直接复制使用4.既然是监控redis的过期key,那必须还需要一个监控类,废话少说,直接整上5.使用redis嘛,还需要一个工具类,整上6.要想真正的实现redis监控,还需要修改redis的配置,将下面图片中的配置文件中的 *notify-keyspace-events* 对应的值改为 *Ex*,notify-keyspace-events "Ex"7.测试类

前言

在网上翻了好久,终于功夫不负有心人,终于让我整出来了,在这里总结一下。SpringBoot项目下,整合redis并对过期key做过期处理。

场景

我负责一个商城系统,发布的优惠券是有有效期的,有效期失效时,优惠券就会变为过期状态,这就需要一个合理的监控者去监控他的有效期是否到了临界值,一旦超过有效期,优惠券便会失效。 我首先想到的是,在数据库做监控,根据资料搜索到可用数据库中的事件去做定时检测,是可以是实现的,但是优惠券的过期时间是精确到秒的,这样数据库做事件监控的话,无疑对数据库来说,会造成很大的压力。所以我果断放弃这种方法。再次查阅资料,便想到了redis监控过期key的方法,用优惠券的id做key,这样就可以很好的解决优惠券过期的问题了。那么废话少说,直接上操作:

1.首先在 pom.xml 文件中引入redis的相关依赖 org.springframework.session spring-session-data-redis 2.然后在 yml 文件中进行配置 redis: #数据库索引 database: 0 host: 127.0.0.1 port: 6379 password: 123456 jedis: pool: #最大连接数 max-active: 15 #最大阻塞等待时间(负数表示没限制) max-wait: -1 #最大空闲 max-idle: 15 #最小空闲 min-idle: 0 #连接超时时间 timeout: 10000 3.使用redis少不了配置类,下面的配置类可以直接复制使用 package cn.xxx.com.common.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * @author Naruto * @date 2024/1/20 19:03 * @description redis配置类 */ @Configuration public class RedisConfiguration { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisMessageListenerContainer redisMessageListenerContainer() { RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory); return redisMessageListenerContainer; } @Bean public KeyExpiredListener keyExpiredListener() { return new KeyExpiredListener(this.redisMessageListenerContainer()); } } 4.既然是监控redis的过期key,那必须还需要一个监控类,废话少说,直接整上 package cn.xxx.com.common.redis; import cn.xxx.com.common.service.machineService.FibMachineCouponService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * @author Naruto * @date 2024/1/20 19:02 * @description redis Key过期监听 */ @Slf4j public class KeyExpiredListener extends KeyExpirationEventMessageListener { @Autowired private FibMachineCouponService fibMachineCouponService; public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expireKey = message.toString(); log.info("过期的key:" + expireKey); //在这里做过期key的处理 } } 5.使用redis嘛,还需要一个工具类,整上 @Slf4j public class RedisUtil { private static RedisTemplate redisTemplate = ContextUtils.getBean("redisTemplate", RedisTemplate.class); /** * 指定缓存失效时间 * * @param key 键 * @param time 时间(秒) * @return */ public static boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { log.error("设置redis指定key失效时间错误:", e); return false; } } /** * 根据key 获取过期时间 * * @param key 键 不能为null * @return 时间(秒) 返回0代表为永久有效 失效时间为负数,说明该主键未设置失效时间(失效时间默认为-1) */ public static long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } /** * 判断key是否存在 * * @param key 键 * @return true 存在 false 不存在 */ public static boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { log.error("redis判断key是否存在错误:", e); return false; } } /** * 普通缓存获取 * * @param key 键 * @return 值 */ @SuppressWarnings("unchecked") public static T get(String key) { return key == null ? null : (T) redisTemplate.opsForValue().get(key); } /** * 普通缓存放入 * * @param key 键 * @param value 值 * @return true成功 false失败 */ public static boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { log.error("设置redis缓存错误:", e); return false; } } /** * 普通缓存放入并设置时间 * * @param key 键 * @param value 值 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 * @return true成功 false 失败 */ public static boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 删除缓存 * * @param key 可以传一个值 或多个 */ @SuppressWarnings("unchecked") public static void remove(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete((Collection) CollectionUtils.arrayToList(key)); } } } /** * 递增 此时value值必须为int类型 否则报错 * * @param key 键 * @param delta 要增加几(大于0) * @return */ public static long incr(String key, long delta) { if (delta if (delta @GetMapping("/test") public AjaxResult test() { log.info("test方法运行了"); long expire = 10L; // 监控key_timeout过期 String key = "123"; log.info("redis过期key:"+key); RedisUtil.set(key,"键123过期了"); String result = RedisUtil.get("123").toString(); log.info("redis过期键绑定的值"+result); RedisUtil.expire(key,expire); return AjaxResult.success("Ok"); } }

测试结果: postman 测试结果 完结撒花,希望我分享的知识能帮助到大家~~



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有